package com.isprint.jslx.base.controller;

import com.isprint.jslx.codemsg.CodeMsg;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;

/**
 * 控制器基类
 * @ClassName ControllerBase
 * @Description 控制器基类
 * @author ruany
 */
public abstract class ControllerBase extends Controller {

	/**
	 * 设置  uuid 对应 attribute name 
	 */
	private String GLOBALATTRIBUTENAME;
	public String getGLOBALATTRIBUTENAME() {
		return GLOBALATTRIBUTENAME;
	}
	public void setGLOBALATTRIBUTENAME(String gLOBALATTRIBUTENAME) {
		GLOBALATTRIBUTENAME = gLOBALATTRIBUTENAME;
	}
	public ControllerBase(){
		init();
	}
	public abstract void init();
	/**
	 * 获取UUID
	 * @return UUID
	 */
	public String getUUID(){
		return this.getRequest().getAttribute(this.GLOBALATTRIBUTENAME)==null?"":this.getRequest().getAttribute(this.GLOBALATTRIBUTENAME).toString();
	}
	/**
	 * 渲染Json返回
	 */
	public void returnJson(){
		String origin = getRequest().getHeader("origin");
		if(!StrKit.isBlank(origin)){
			if (origin.indexOf("gyeducation.com") != -1){
				this.getResponse().addHeader("Access-Control-Allow-Origin", origin);
			}else{
				this.getResponse().addHeader("Access-Control-Allow-Origin", null);
			}
		}
		this.renderJson();
	}
	
	public void returnJson(String json){
		String origin = getRequest().getHeader("origin");
		if(!StrKit.isBlank(origin)){
			if (origin.indexOf("gyeducation.com") != -1){
				this.getResponse().addHeader("Access-Control-Allow-Origin", origin);
			}else{
				this.getResponse().addHeader("Access-Control-Allow-Origin", null);
			}
		}
		this.renderJson(json);
	}
	
	/**
	 * 根据状态key获取对应提示信息 
	 * @param key 
	 * @return value
	 */
	public String getResult(String key){
		return CodeMsg.STATUS_CODE_MAP.get(key)==null?"没有"+key+"对应的状态码":CodeMsg.STATUS_CODE_MAP.get(key);
	}
	
	/**
	 * 根据状态码获取对应提示信息 
	 * @param status 1-16
	 * @return String
	 */
	public String getResult(int status){
		return CodeMsg.STATUS_CODE_MAP.get(status+"")==null?"没有"+status+"对应的状态码":CodeMsg.STATUS_CODE_MAP.get(status+"");
	}

	private String ROUTEPATH;
	public String getROUTEPATH() {
		return ROUTEPATH;
	}
	public void setROUTEPATH(String rOUTEPATH) {
		ROUTEPATH = rOUTEPATH;
	}

	/**
	 * 得到转义后的string,防止xss攻击
	 * @param name
	 * @return
	 */
//	public String getParaWithoutHtml(String name){
//		return Tools.filterXSS(getPara(name));
//	}

}
